// 文件名：week1\problem\lc209_欧琳琳.cpp
// 提交链接：https://leetcode.cn/problems/minimum-size-subarray-sum/submissions/559393287?envType=problem-list-v2&envId=sliding-window&status=SOLVED
// 209. 数组，二分查找，前缀和，滑动窗口
// 完成日期：2024/08/28
// c++,使用滑动窗口来找最小子数组长度，通过end和start两个指针动态调整窗口大小

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n=nums.size();
        int start=0;
        int sum=0;
        int minlength=INT_MAX;
        for(int end=0;end<n;end++)
        {
            sum+=nums[end];
            while(sum>=target)
            {
                minlength=min(minlength,end-start+1);
                sum-=nums[start];
                start++;
            }
        }
        return minlength==INT_MAX?0:minlength;;
    }
};